/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.UserDao;
import com.qdu.entity.Admin;
import com.qdu.entity.User;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sanshi
 */
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

    /**
     * 按照用户名和密码查询信息
     *
     * @param user
     * @return
     */
    @Override
    public User getUserByUserNameAndPassWord(User user) {
        String Md5_pwd = "null";
        User bean = null;
        try {
            Md5_pwd = toMD5(user.getUpwd());
            String sql = "select * from [dbo].[User] where Uname =? and Upwd = ? ";
            bean = this.getBean(sql, user.getUname(), Md5_pwd);

        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
//        String sql = "select * from [dbo].[User] where Uname =? and Upwd = ? ";
//        User bean = this.getBean(sql, user.getUname(), Md5_pwd);
//        bean.setUpwd(user.getUpwd());
        return bean;
    }

    @Override
    public User getRestCode(User user) {
        String sql = "select * from [dbo].[User] where Uname =? and Uemail = ? ";
        User bean = this.getBean(sql, user.getUname(), user.getUemail());
        return bean;
    }
    
    
      @Override
    public boolean resetPwd(User user) {
         String psw_md5 = "MD5";
        try {
            psw_md5 = toMD5(user.getUemail());
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("修改信息中加密后的密码：" + psw_md5);
        String sql = "UPDATE [dbo].[User] set  [Upwd]=? WHERE [Upwd]=?";
        int update = this.update(sql, psw_md5,user.getUname());
        System.out.println(update);
        return update > 0;
    }
//  @Override
//    public Admin getAdminByUserNameAndPassWord(Admin admin) {
//          String sql = "select [id],[Aname],[Apwd] from [dbo].[User] where Aname =? and Apwd = ? ";
//        Admin bean = this.getBean(sql, admin.getAneme(), admin.getApwd());
//        return bean;
//    }

    /**
     * 注册 保存用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean registUser(User user) {
        String psw_md5 = "MD5";
        try {
            psw_md5 = toMD5(user.getUpwd());
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("注册中加密后的密码：" + psw_md5);
        String sql = "INSERT INTO [dbo].[User] ( [Uname], [Upwd],[Uemail],[Uicon]) VALUES ( ?,?, ?,'default.jpg')";

        int update = this.update(sql, user.getUname(), psw_md5, user.getUemail());
        System.out.println(update);
        return update > 0;
    }

    @Override
    public boolean fixInfo(User user) {
        String psw_md5 = "MD5";
        try {
            psw_md5 = toMD5(user.getUpwd());
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("修改信息中加密后的密码：" + psw_md5);
        String sql = "UPDATE [dbo].[User] set  [Upwd]=?,[Uemail]=?,[Uicon]=?,[Uphone]=?,[Ugender]=?,[Ubirth]=?,[Uarea]=? WHERE [Uname]=?";
        int update = this.update(sql, psw_md5, user.getUemail(), user.getUicon(), user.getUphone(), user.getUgender(), user.getUbirth(), user.getUarea(), user.getUname());
        System.out.println(update);
        return update > 0;
    }

    //登陆判断
    public boolean isLogin(String old, String md5) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        boolean flag = false;
        String md = toMD5(old);
        if (md.equals(md5)) {
            flag = true;
        }
        return flag;
    }

    //md5加密
    public String toMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] strByteArray = str.getBytes("utf-8");
        byte[] mdByteArray = md.digest(strByteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < mdByteArray.length; i++) {
            int val = ((int) mdByteArray[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

  

}
